using System.Collections.Generic;
using System;
namespace DBManager
{
    partial class Point
    {
        public int pointLeft
        {
            get
            {
                int pointAcq = 0;
                int pointUsed = 0;
                if (this.PointsAcquired != null)
                {
                    pointAcq = (int)this.PointsAcquired;
                }

                if (this.PointsUsed != null)
                {
                    pointUsed = (int)this.PointsUsed;
                }




                return pointAcq - pointUsed;
            }
        }
    }

    partial class OutputColumn
    {
        public bool isNumber
        {
            get
            {
                if(this.NumberFormat.Contains("#") || this.NumberFormat.Contains("0"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

    }

    partial class ChartOutput
    {
        public double valueFormat
        {
            get 
            {
                double a =  (double)Convert.ToDouble(this.OutputValue);
                return a;
            }
        }
    }

    partial class OutputValue
    {

    }

    partial class InputValue
    {
        public string valueFormat
        {
            get
            {

                InputDesign input = DBManager.Instance.GetInputDesign(this.FieldID, this.CalculationID);

                string valStr = "";
                if (input.NumberFormat == "0.00%" || input.NumberFormat[0] == '#' || input.NumberFormat == "#,##0.00" || input.NumberFormat.Contains("#") || input.NumberFormat.Contains("0"))
                {
                    string defaultVal = this.InputValue1;
                    decimal value = Convert.ToDecimal(defaultVal);

                    valStr = value.ToString(input.NumberFormat);

                    if (value == (decimal)0.00)

                        valStr = "";

                    return valStr;

                }

                if (this.InputValue1 == "0")
                    return "";

                return this.InputValue1;
            }

        }
    }

    partial class User
    {
        public List<Calculation> CalculationList(Customer customer)
        {
            List<Calculation> calList = new List<Calculation>();

            List <Access> accList = DBManager.Instance.GetAcesss(this.UserID);

            Dictionary<int, Calculation> set = new Dictionary<int, Calculation>();

            foreach (Access item in accList)
            {

               if(item.FirstTimePaid == true || (item.ExpiryDate == null || item.ExpiryDate != null ) )
               {

                   Calculation  calc = DBManager.Instance.GetCalculation(item.CalculationID);

                   if (calc != null)
                   {
                       calList.Add(calc);
                       set.Add(calc.ID, calc);
                   }

               } 
            }

            foreach (Calculation item in customer.CalculationList)
            {
                if (set.ContainsKey(item.ID) == false && item.Private == null)
                {
                    calList.Add(item);
                }
            }



            return calList;

        }

        public bool CanAccess(Calculation calc)
        {
            Access access = DBManager.Instance.GetAcesss(this.UserID, calc.CalculationID);

            if (calc.isFreeCalculation)
                return true;

            if (access == null)
            {
                return false;
            }

            if (calc.PointsPerPeriod != null && DateTime.Today > access.ExpiryDate)
            {
                return false;
            }

            if (calc.PointsFirstTime != null && access.FirstTimePaid != true)
            {
                return false;
            }


            if (calc.PointsPerCalc != null)
            {
                Point point = DBManager.Instance.GetPoint(this.UserID, calc.CalculationID);

                if (point == null || point.pointLeft < calc.PointsPerCalc)
                {
                    return false;
                }

            }

            return true;
        }
    }

    partial class OrderControl
    {
        public string OrderName
        {
            get
            {
                string orderName = this.OrderID.Substring(0, 4);
                orderName += "-";
                orderName += this.OrderID.Substring(4, 2);
                orderName += "-";
                orderName += this.OrderID.Substring(6, 2);
                orderName += " ";
                orderName += this.OrderID.Substring(8, 8);

                return orderName;
            }
        }

        public System.Collections.Generic.Dictionary<string, string> InputDicts
        {
            get
            {
                System.Collections.Generic.Dictionary<string, string> inputValues = new Dictionary<string, string>();

                foreach (InputValue item in this.InputValues)
                {
                    InputDesign input = DBManager.Instance.GetInputDesign(item.FieldID, item.CalculationID);
                    string value = item.InputValue1;


                    inputValues.Add(item.FieldID, item.valueFormat);
                }

                return inputValues;
            }
        }
    }

    partial class InputDesign
    {
         public List<InputDesignList> InputList
        {
            get
            {
                return DBManager.Instance.GetInputListItemDesign(this.CalculationID, this.ListID);
            }
        }

         public string defaultValueFormat
         {
             get
             {

                 string valueStr = "";
                 if (this.NumberFormat != null && (this.NumberFormat == "0.00%" || this.NumberFormat[0] == '#' || this.NumberFormat == "#,##0.00" || this.NumberFormat.Contains("#") || this.NumberFormat.Contains("0")))
                 {
                     string defaultVal = this.DefaultValue;                    
                     decimal value = Convert.ToDecimal(defaultVal);

                     valueStr = value.ToString(this.NumberFormat);

                     if (defaultVal == "0")
                         valueStr = "";

                     return valueStr;

                 }



                 if (this.DefaultValue == null)
                     return "";

                 if (this.DefaultValue == "0")
                 {
                     return "";
                 }

                 valueStr = this.DefaultValue;



                 return valueStr;

             }

         }

         public bool isNumber
         {
             get
             {
                 if (this.FieldID != null && this.NumberFormat != null && (this.NumberFormat == "0.00%" || this.NumberFormat[0] == '#' || this.NumberFormat == "0.00" || this.NumberFormat.Contains("#") || this.NumberFormat.Contains("0")))
                 {
                     return true;
                 }

                 return false;
             }
         }

         public bool isPercent
         {
             get
             {
                 if (this.FieldID != null && this.NumberFormat != null && (this.NumberFormat == "0.00%" || this.NumberFormat.Contains("%")))
                 {
                     return true;
                 }

                 return false;
             }
         }

         public int nDp
         {
             get
             {
                 
                 char[] sep = new char[]{'.'};

                 if (this.NumberFormat == null)
                     return 0;

                 if (this.NumberFormat.Contains("."))
                 {
                     string[] str = this.NumberFormat.Split(sep);

                     return str[str.Length - 1].Length;
                 }
                 else if (this.NumberFormat.Contains("%") && this.NumberFormat.Contains("."))
                 {

                     string[] str = this.NumberFormat.Split(sep);

                     return str[str.Length - 2].Length;
                 }

                 return 0;
             }
         }

         public bool isDateTime
         {
             get
             {
                 if (this.FieldID != null && this.NumberFormat != null && (this.NumberFormat == "m/d/yyyy" || this.NumberFormat == "d/m/yyyy" || this.NumberFormat.Contains("dd")))
                 {
                     return true;
                 }

                 return false;
             }
         }
    }

    partial class InputDesignGroup
    {
        public string grpName
        {
            get
            {
                if (this.GroupDescription == null)
                    return "";
                else
                    return this.GroupDescription;
            }
        }

        public List<Column> ColumnList
        {
            get
            {
                return DBManager.Instance.GetColumns(this.GroupID, this.CalculationID);
            }

        }

        public List<InputDesign> inputDesignList
        {
            get
            {
                return DBManager.Instance.GetInputDesignList(this.CalculationID,this.GroupID);
            }
        }



    }

    partial class OutputDesignGroup
    {
        public string grpName
        {
            get
            {
                if (this.GroupDescription == null)
                    return "";
                else
                    return this.GroupDescription;
            }
        }
        /*
        public List<OutputColumn> OutputColumnList
        {
            get
            {
                return DBManager.Instance.GetOutputColumns(this.CalculationID);
            }
        }
        */
        public List<OutputDesign> outputDesignList
        {
            get
            {

                return DBManager.Instance.GetOutputDesignList(this.CalculationID, this.GroupID);
            }
        }
    }

    partial class OutputDesign
    {
        public List<OutputColumn> OutputColumnList
        {
            get
            {
                return DBManager.Instance.GetOutputColumns(this.CalculationID, this.FieldID);
            }
        }

        public bool isNumber
        {
            get
            {
                if (this.NumberFormat == "0.00%" || this.NumberFormat[0] == '#' || this.NumberFormat == "0.00" || this.NumberFormat.Contains("#") || this.NumberFormat.Contains("0"))
                {
                    return true;
                }

                return false;
            }
        }
    }

    partial class Calculation
    {
        public List<InputDesignGroup> InputDesignGroupsList
        {
            get
            {
                
                return DBManager.Instance.GetInputDesignGroups(this._CalculationID);
            }
        }

        public string TextNewCalc
        {
            get
            {
                if (TextNewCalculation == null)
                {
                    return "New Calculation";
                }
                else
                {
                    return TextNewCalculation;
                }
            }
        }

        public string TextLatestCalc
        {
            get
            {
                if (TextLatestCalculation == null)
                {
                    return "Latest Calculation";
                }
                else
                {
                    return TextLatestCalculation;
                }
            } 
        }

        public string TextPickList
        {
            get
            {
                if (TextPickCalcFromList == null)
                {
                    return "Pick From List";
                }
                else
                {
                    return TextPickCalcFromList;
                }
            } 
        }

        public List<OutputDesignGroup> OutputDesignGroupsList
        {
            get
            {
                //DBManager.Instance.mutexlock();

                List<OutputDesignGroup> list =  DBManager.Instance.GetOutputDesignGroups(this._CalculationID);

                //DBManager.Instance.mutexUnlock();

                return list;
            }
        }

        public bool isFreeCalculation
        {
            get
            {
                if (this.PricePerCalc == null && this.PointsPerCalc == null && this.PointsPerPeriod == null)
                {
                    return true;
                }

                return false;
            }
        }
    }

    partial class Customer
    {


        public List<Calculation> CalculationList
        {
            get
            {
                List<Calculation> calList = new List<Calculation>();

                
                foreach (Calculation item in this.Calculations)
                {

                    if (item.Approved == 1 && item.Private != true)
                        calList.Add(item);
                }

                return calList;
            }

        }
            
          
    }
}
